﻿using AutoMail.Email;
using AutoMail.Entity;
using AutoMail.Quartz;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace AutoMail.Controllers.Test
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class TestAppServices : ControllerBase
    {
        private readonly ISqlSugarClient _db;
        private readonly IJobServices _jobServices;
        private readonly IEmailHelper _emailHelper;

        public TestAppServices(ISqlSugarClient db, IJobServices job,IEmailHelper emailHelper)
        {
            _db = db;
            _jobServices = job;
            _emailHelper=emailHelper;
        }


        [HttpPost]
        public async Task CodeFirst()
        {
            _db.DbMaintenance.CreateDatabase();
            _db.CodeFirst.SetStringDefaultLength(255).InitTables(typeof(TestModel));
            await Task.CompletedTask;
        }

        /// <summary>
        /// s秒后输出文本
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task CreateJob(int s)
        {
            await _jobServices.CreateJob(DateTime.Now.AddSeconds(s), () =>
            {
                Console.WriteLine("任务执行");
            });
        }

        /// <summary>
        /// 每隔s秒输出文本
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task CreatePollJob(int s)
        {
            await _jobServices.CreatePollJob(DateTime.Now.AddSeconds(s), TimeSpan.FromSeconds(s), () =>
            {
                Console.WriteLine("文本");
            });
        }

        /// <summary>
        /// 获取所有正在执行的任务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<List<string>> GetAllJobs()
        {
            var result = await _jobServices.GetAllJobs();
            return result;
        }

        /// <summary>
        /// 测试邮件发送
        /// </summary>
        /// <param name="titile"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task SendEmailTest(string titile,string context)
        {
            await _emailHelper.SendEmail(new EmailContextDto {Title=titile,Context=context,reciver= "liuxue@yinzhierp.com" });
        }

        [HttpPost]
        public async Task TestMehtod()
        {
            List<string> result = ["123","456","789"];
            var sql=_db.Queryable<TestModel>().Where(x => result.Contains(x.Name)).ToSql();
            await Console.Out.WriteLineAsync(sql.Key);
            await Task.CompletedTask;
        }
    }
}